<?php

require '../vendor/autoload.php';

use Gnway\Util\Concurrent\Process;
use Gnway\Util\Concurrent\Runnable;
use Gnway\Util\Concurrent\WaitException;

$process1 = new Process(new class implements Runnable {
  public function run(): void
  {
    while (!Process::currentProcess()->isInterrupted()) {
      echo 'Pid: ', getmypid(), PHP_EOL;
      sleep(1);
    }
  }
});

$process2 = new class extends Process {
  public function run(): void
  {
    while (!$this->isInterrupted()) {
      echo 'Pid: ', getmypid(), PHP_EOL;
      sleep(1);
    }
  }
};

$process1->start();
$process2->start();

try {
  $process1->wait(3);
} catch (WaitException $ex) {
  echo 'Process[' . $process1->getPid() . ']' . $ex->getMessage() . PHP_EOL;
  $process1->shutdown();
}

try {
  $process2->wait(3);
} catch (WaitException $ex) {
  echo 'Process[' . $process2->getPid() . ']' . $ex->getMessage() . PHP_EOL;
  $process2->shutdown();
}

echo 'Process has been quit', PHP_EOL;

exit(0);
